#!/usr/bin/python3
 # 邮箱POP3/SMTP授权码: qjadngqpktngbgad    cpgqqpnyewygbbdc
 # 邮箱IMAP/SMTP授权码:  wehrvsuuvyhdbdde
import smtplib, time
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage


class OCR_EMAIL():
    def __init__(self):
        self.my_sender = '754155003@qq.com'  # 发件人邮箱账号
        self.my_pass = 'kpdpwskicnkjbgad'  # 发件人邮箱密码(当时申请smtp给的口令)
        self.my_user = '1315862333@qq.com'  # 收件人邮箱账号，我这边发送给自己r
        self.error_str = None     # 错误类型

    def mail(self, RMC_bar, serial_number, Fail_bar_obj, email_type, sinOUT):
        ret = True
        try:
            if email_type == 1:   # 高拍仪记录
                url_str = "http://127.0.0.1:9527/OutTicket&bar=%sserial_number=%s" % (RMC_bar, serial_number)
                label_str = r"<p>部番: %s, 出张出库票: %s, 厂家: %s, 区域代码: %s, 线名: %s, 工位: %s</p><p>%s</p>" %\
                            (RMC_bar, str(Fail_bar_obj["ticket_bar"]), str(Fail_bar_obj["plant"]), Fail_bar_obj["area_code"], Fail_bar_obj["lineName"], Fail_bar_obj["station"], Fail_bar_obj["error"])

            elif email_type == 0:   # OCR扫描记录
                url_str = "http://127.0.0.1:9527/get_scan_log&bar=%sserial_number=%s" % (RMC_bar, serial_number)
                # http: // 10.10.1.2: 9527 / get_scan_log & bar = D1497982C - +-Pserial_number = 2021040315094088
                label_str = r"<p>部番: %s, 唯一码: %s</p><p>%s</p>" % \
                            (RMC_bar, str(Fail_bar_obj["serial_number"]), Fail_bar_obj["error"])

            elif email_type == 2:
                for RMC_dict in Fail_bar_obj:
                    url_str = "http://127.0.0.1:9527/OutTicket&ticket_bar=%sserial_number=%s" % (RMC_dict["ticket_bar"], serial_number)
                    label_str = r"<p>出张出库票: %s, 厂家: %s, 区域代码: %s, 线名: %s</p><p>%s</p>" %\
                            (RMC_dict["ticket_bar"], RMC_dict["plant"], RMC_dict["area_code"], RMC_dict["lineName"], RMC_dict["error"])
                    break

            msg = MIMEMultipart('mixed')
            msg['From'] = formataddr(["九点半", self.my_sender])
            # 括号里的对应发件人邮箱昵称、发件人邮箱账号
            msg['To'] = formataddr([self.my_user, self.my_user])
            # 括号里的对应收件人邮箱昵称、收件人邮箱账号
            msg['Subject'] = 'OCR异常'

            mail_msg = """%s<p><a href=%s rel="external nofollow" >点击获取部番详情</a></p>""" % (label_str, url_str)

            text = MIMEText(mail_msg, 'html', 'utf-8')    # 文本模式
            # msg=MIMEText('<邮件内容>','plain','utf-8')  # msg = MIMEText(<邮件内容>, 'base64', 'utf-8')  文件模式TXT
            msg.attach(text)

            # 构造图片链接  发送照片
            sendimagefile = open(r'E:\OCR_RMC\ORC_BAR2.0\EMAIL\01.png', 'rb').read()
            image = MIMEImage(sendimagefile)
            image.add_header('Content-ID', '<image1>')
            image["Content-Disposition"] = 'attachment; filename="testimage.png"'
            msg.attach(image)
            # 邮件的主题，也可以说是标题
            server = smtplib.SMTP_SSL("smtp.qq.com", 465)
            # 发件人邮箱中的SMTP服务器，端口是465
            server.login(self.my_sender, self.my_pass)
            # 括号中对应的是发件人邮箱账号、邮箱密码
            server.sendmail(self.my_sender, [self.my_user, ], msg.as_string())
            # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
            server.quit()  # 关闭连接
        except Exception as e:  # 如果 try 中的语句没有执行，则会执行下面的 ret=False
            # sinOUT.emit("result&邮件发送异常")
            ret = False
            # sinOUT.emit("result&%s" % e)
        return ret


if __name__=="__main__":
    obj = OCR_EMAIL()
    RMC_bar = "123456"
    serial_number = 3456789
    Fail_bar_obj = 567890
    email_type = 1
    obj.mail(RMC_bar, serial_number, Fail_bar_obj, email_type)









